home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / prog_c / cuj0696.zip / DWYER.ZIP / LIB / KSCALC.C < prev    next >
C/C++ Source or Header  |  1996-03-30  |  2KB  |  64 lines

  1. /* ============ */
  2. /* kscalc.c    */
  3. /* ============ */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <math.h>
  7. #include <mconf.h>
  8. #include <miscdefs.h>
  9.  
  10. static  int  DblCmprFun(const void *Key, const void *Elem)
  11. {
  12.     return (*(double *) Key > *(double *) Elem) ?  1 :
  13.        (*(double *) Key < *(double *) Elem) ? -1 : 0;
  14. }
  15. /* ==================================================================== */
  16. /* KSCalc - Calculates Kolmogorov-Smirnov Statistics and Probabilities    */
  17. /* ==================================================================== */
  18. void
  19. KSCalc(double KSArray[], int ArraySize, double *KPStat, double *KPProb,
  20.     double *KMStat, double *KMProb)
  21. {
  22.     int        k;
  23.     double  MaxKnMinus, MaxKnPlus;
  24.  
  25.     /* -------------------------------------------------------- */
  26.     /* Expressions for the Kolmogorov-Smirnov Statistics and    */
  27.     /* Probabilities Can Be Found in Knuth, Donald E., The Art    */
  28.     /* of Computer Programming, Vol 2, Second Edition, 1981    */
  29.     /* -------------------------------------------------------- */
  30.  
  31.     /* -------------------------------------------------------- */
  32.     /* Calculate K-S on Chi-Square Statistics and Probabilities */
  33.     /* -------------------------------------------------------- */
  34.  
  35.     /* ----------------------------- */
  36.     /* Sort Array in Ascending Order */
  37.     /* ----------------------------- */
  38.     qsort(KSArray, ArraySize, sizeof(double), DblCmprFun);
  39.  
  40.     MaxKnMinus = MaxKnPlus = -100000.0;
  41.     for (k = 1; k <= ArraySize; ++k)
  42.     {
  43.     double  NextKn;
  44.  
  45.     /* ---------------------- */
  46.     /* Find Final Kn+ and Kn- */
  47.     /* ---------------------- */
  48.     NextKn = (double)k/(double)ArraySize - KSArray[k-1];
  49.     MaxKnPlus = __max(MaxKnPlus, NextKn);
  50.  
  51.     NextKn = 1.0/(double)ArraySize - NextKn;
  52.     MaxKnMinus = __max(MaxKnMinus, NextKn);
  53.     }
  54.  
  55.     /* -------------------------------------------------- */
  56.     /* Store Statistics and Probabilities for Kn+ and Kn- */
  57.     /* -------------------------------------------------- */
  58.     *KPStat = MaxKnPlus > 0 ? MaxKnPlus : 0;
  59.     *KPProb = KSmirnov(ArraySize, *KPStat);
  60.  
  61.     *KMStat = MaxKnMinus > 0 ? MaxKnMinus : 0;;
  62.     *KMProb = KSmirnov(ArraySize, *KMStat);
  63. }
  64.